/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import com.bourre.data.collections.Map;
import com.bourre.events.BasicEvent;

import fever.app.context.abstract.ContextContainer;
import fever.app.context.abstract.Dialog;
import fever.app.context.abstract.FeverContext;
import fever.app.context.abstract.FileSystem;
import fever.app.context.abstract.IContextFactory;
import fever.app.context.haxe.HaxeContainer;
import fever.app.context.haxe.HaxeDialog;
import fever.app.context.haxe.HaxeFileSystem;
import fever.app.context.haxe.HaxeMethod;
import fever.app.local.Localisation;
import fever.core.ApplicationState;
import fever.events.ApplicationEvent;
import fever.events.FeverController;
import fever.events.FeverEventList;
import fever.Fever;
import fever.log.FeverDebug;

import swhx.Api;

/**
 * Builds a <strong>Haxe context</strong> for application.
 * 
 * @see fever.app.context.ContextManager
 * 
 * @author Romain Ecarnot
 */
class fever.app.context.haxe.HaxeContext extends FeverContext implements IContextFactory
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private static var _instance : HaxeContext;
	
	private var _map : Map;
	
	
	//-------------------------------------------------------------------------
	// Public Properties
	//-------------------------------------------------------------------------
	
	/** Remoting connection between Haxe and Flash GUI. */
	public static var HAXE_REMOTING : String = 'feverhx';
	
	/** Contstant. Haxe method name for application.exit() method. */
	public static var exitMETHOD : HaxeMethod = new HaxeMethod( 'exit' );
	
	/** Contstant. Haxe method name for runProcess() method. */	public static var runProcessMETHOD : HaxeMethod = new HaxeMethod( 'runProcess' );
	
	/** Contstant. Haxe method name for context runtime checking process. */
	public static var isHaxeMETHOD : HaxeMethod = new HaxeMethod( 'isHaxe' );
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Returns Haxe backend method name.
	 * 
	 * @param method Method name to call
	 */
	public static function getHaxeMethod( method : HaxeMethod ) : String
	{
		return HAXE_REMOTING + '.' +  method.toString();
	}
	
	/**
	 * Returns {@link fever.app.context.ContextManager} instance.
	 * 
	 * <p>Use {@link #init} method to define context.
	 */
	public static function getInstance( ) : HaxeContext
	{
		if( !_instance ) _instance = new HaxeContext( );
		return _instance;
	}
	
	/**
	 * Creates Haxe application container.
	 */
	public function createContainer() : ContextContainer
	{
		return new HaxeContainer();
	}
	
	/**
	 * Inits Haxe FileSystem I/O.
	 */
	public function createFileSystem() : FileSystem
	{
		return new HaxeFileSystem();
	}
	
	/**
	 * Creates dialog system.
	 */
	public function createDialog() : Dialog
	{
		return new HaxeDialog();
	}
	
	/**
	 * Opens a external document.
	 */
	public function openDocument( path : String ) : Void
	{
		//TODO HaxeContext#openDocument()
	}
	
	/** 
	 * Run an external application.
	 */
 	public function runProcess( app : String, args : String, workingDir : String, hidden : Boolean, waitFor : Boolean )
 	{
 		return Api.call( 
			HaxeContext.getHaxeMethod( runProcessMETHOD ), 
			app,
			args,
			( workingDir ) ? workingDir : null,
			hidden,
			waitFor
		); 
 	}
 	
	/** 
	 * Quit application.
	 */
	public function quit( force : Boolean ) : Void
	{
		if( force == true ) _exit( true );
		else
		{
			var o : FeverController = FeverController.getInstance();
			if( o.isRegistred( FeverEventList.onExitApplicationEVENT ) )
			{
				FeverController.getInstance().broadcastEvent( new BasicEvent( FeverEventList.onExitApplicationEVENT ) );
			}
			else
			{
				_exit( false );
			}
		}
	}
 	 	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	private function HaxeContext()
	{
		super();
		
		Api.init( this );
		
		_initStateMap();
	}
	
	private function _initStateMap() : Void 
	{
		_map = new Map();
		_map.put( -1, ApplicationState.MINIMIZED );
		_map.put( 0, ApplicationState.NORMAL );
		_map.put( 1, ApplicationState.MAXIMIZED );
		_map.put( 2, ApplicationState.FULLSCREEN );
	}
	
	private function _exit( force : Boolean ) : Void
	{
		Api.call( 
			getHaxeMethod( exitMETHOD ),
			force, 
			Fever.application.getTitle(), 
			Localisation.getResource( 'fever:common.quitMessage' )
		 );	
	}
	
	/**
	 * Calls by Haxe
	 */
	private function onQuit() : Void
	{
		var o : FeverController = FeverController.getInstance();
		
		if( o.isRegistred( FeverEventList.onExitApplicationEVENT ) )
		{
			FeverController.getInstance().broadcastEvent( new BasicEvent( FeverEventList.onExitApplicationEVENT ) );
		}
		else Fever.application.quit();
	}
	
	//TODO retreive rtti infos for runtime hx methods checking
	private function loadInfos( rtti : String ) : Void
	{
		FeverDebug.DEBUG( 'hx rtti received' );
	}
	
	/**
	 * Calls by Haxe
	 * 
	 * //TODO HaxeContainer.onFocus()
	 */
	private function onFocus( b : Boolean ) : Void
	{
		Fever.application.broadcastEvent( 
			new ApplicationEvent( 
				ApplicationEvent.onFocusChangeEVENT,  
				b
			)
		);
	}
	
	private function onState( state : Number ) : Void
	{
		if( !_map.containsKey( state ) ) state = ApplicationState.NORMAL;
		
		if( state != Fever.application.state )
		{
			Fever.application.broadcastEvent( 
				new ApplicationEvent( 
					ApplicationEvent.onStateChangeEVENT,  
					_map.get( state )
				) 
			);	
		}
	}
}